import requests
from lxml import etree

# 快代理后台生成的api链接   5s访问一次
api_url = "https://dps.kdlapi.com/api/getdps/?secret_id=olrejp12b94qknshw4be&signature=m1y23gc6dg9ivkljppeovaaprqkqdgdh&num=1&pt=1&sep=1"

# 获取API接口返回的代理IP
proxy_ip = requests.get(api_url).text

print("获取新IP地址：", proxy_ip)

# 订单生成的账户与密码
username = "d2356162166"
password = "l7zee1jn"
# 代理
proxies = {
    "http": f"http://{username}:{password}@{proxy_ip}",
    "https": f"http://{username}:{password}@{proxy_ip}",
}

for page in range(1, 100 + 1):
    url = f"https://zz.lianjia.com/zufang/pg{page}/#contentList"
    headers = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36"
    }
    response = requests.get(url, headers=headers, proxies=proxies)
    result = response.content.decode()
    root = etree.HTML(result)
    tag_list = root.xpath("//div[@class='content__list--item']")
    print(len(tag_list))
    for tag in tag_list:
        title = tag.xpath(".//p[contains(@class,'content__list--item--title')]/a/text()")[0]
        title = title.strip()
        url = tag.xpath(".//p[contains(@class,'content__list--item--title')]/a/@href")[0]
        price = tag.xpath(".//span[contains(@class,'content__list--item-price')]//text()")
        price = "".join(price)
        print(page, title, url, price)
